window.onload=function(){
    chooseProduct();
    changeCount();
    totalPrice();
} ;

function chooseProduct() {
    var allChoose=document.getElementById("all-choose");
    var chooses=document.getElementsByClassName("choose");
    var isChoose=true;

    allChoose.onclick=function () {
        if(!isChoose){
            for(var i=0;i<chooses.length;i++){
                chooses[i].style.backgroundPosition="0 -64px";
                isChoose=true;
            }
        }else{
            for(var i=0;i<chooses.length;i++){
                chooses[i].style.backgroundPosition="0 -44px";
                isChoose=false;
            }
        }
    }

    for(var i=1;i<chooses.length;i++){
        chooses[i].onclick=function () {
            if(!isChoose){
                this.style.backgroundPosition="0 -64px";
                isChoose=true;
            }else{
                this.style.backgroundPosition="0 -44px";
                isChoose=false;
            }
        }
    }
}

function changeCount() {
    var regs=document.getElementsByName("reg");
    var adds=document.getElementsByName("add");
    var counts=document.getElementsByName("count");

    for(var i=0;i<regs.length;i++){
        regs[i].index=i;
        regs[i].onclick=function () {
            var a=this.index;
            if(parseInt(counts[a].value)==1){
                return;
            }
            counts[a].value=parseInt(counts[a].value)-1;
            totalPrice();
        };
        adds[i].index=i;
        adds[i].onclick=function () {
            var a=this.index;
            counts[a].value=parseInt(counts[a].value)+1;
            totalPrice();
        }
    }
}

function totalPrice() {
    var prices=document.getElementsByClassName("price");
    var counts=document.getElementsByName("count");
    var danjia=document.getElementsByClassName("danjia");
    var total;
    var totalPrice=0;

    for(var i=0;i<prices.length;i++){
        // prices[i].index=i;
        total=parseFloat(danjia[i].value)*parseInt(counts[i].value);
        prices[i].innerHTML=total.toFixed(2);
        totalPrice=totalPrice+parseFloat(prices[i].innerHTML);
    }

    var detailTotalPrice=document.getElementsByClassName("detail-total-price")[0];
    var orderDetailPrice=document.getElementsByClassName("order-detail-price")[0];

    detailTotalPrice.innerHTML="&yen;"+totalPrice.toFixed(2);
    orderDetailPrice.innerHTML="&yen;"+totalPrice.toFixed(2);

}